package com.aduerr.v1shouyin.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.util.SecrectAction;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.orhanobut.hawk.Hawk;
import com.yeahka.shouyintong.sdk.action.LockPos;
import com.yeahka.shouyintong.sdk.api.SytApi;
import com.yeahka.shouyintong.sdk.api.SytFactory;


public class LoadingActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            //结束你的activity
            finish();
            return;
        }
        setContentView(R.layout.activity_loading);
        switchActivityForSkip();

    }



    /**
     * 1.判断是否绑定了Guid?  绑定了 跳转到登录界面                    没有绑定的话,跳转到登录界面
     */
    private void switchActivityForSkip() {
        SecrectAction secrectInstance = new SecrectAction(LoadingActivity.this);
        String DeviceID = secrectInstance.queryData();
        if(!TextUtils.isEmpty(DeviceID)) {
            Hawk.put("DeviceID", DeviceID);
        }
        secrectInstance.closeDB();

        String deviceId = Hawk.get("DeviceID");

        //判断数据库里是否存储了deviceID
        if(!TextUtils.isEmpty(DeviceID)) {
            Boolean autologin =
                    (Boolean) SharedPreferencesUtils.getParam(LoadingActivity.this,
                            "autologin", false);
            if(autologin) {
                if(Hawk.contains("agenttel")) {
                    Intent intent = new Intent(LoadingActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(LoadingActivity.this,
                            HomePageActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else {
                Intent intent = new Intent(LoadingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        } else if(!TextUtils.isEmpty(deviceId)) {
            Boolean autologin =
                    (Boolean) SharedPreferencesUtils.getParam(LoadingActivity.this,
                            "autologin", false);
            if(autologin) {
                if(Hawk.contains("agenttel")) {
                    Intent intent = new Intent(LoadingActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Intent intent = new Intent(LoadingActivity.this,
                            HomePageActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else {
                Intent intent = new Intent(LoadingActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        } else {//没有绑定guid
            Intent intent = new Intent(LoadingActivity.this, BangGuidActivity.class);
            startActivity(intent);
            finish();
        }

    }




}
